package com.tsunami.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsunami.auth.service.auth.IResourceService;
import com.tsunami.auth.service.auth.IUserService;
import com.tsunami.authority.dto.auth.*;
import com.tsunami.authority.entity.auth.User;
import com.tsunami.base.R;
import com.tsunami.log.annotation.SysLog;
import com.tsunami.user.annotation.LoginUser;
import com.tsunami.user.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2021-11-17 14:02
 * @Created by lx
 */
@RestController
@Api(tags = "UserController", description = "用户管理")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IResourceService resourceService;

    @GetMapping("/page")
    @ApiOperation(notes = "用户列表（分页）",value = "获取用户列表")
    @SysLog("获取用户列表")
    public R page(Integer pageNum,
                  Integer pageSize,
                  String orderByColumn,
                  String isAsc,
                  UserDTO user){
        return userService.selectPageList(user);
    }

    @GetMapping("/get")
    @ApiOperation(notes = "查询用户",value = "查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long", paramType = "query", defaultValue = ""),
    })
    @SysLog("查询用户")
    public R get(Long id){
        User user = userService.getById(id);
        return R.ok().only(user);
    }

    @GetMapping("/info")
    @ApiOperation(notes = "用户信息",value = "登录用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long", paramType = "query", defaultValue = ""),
    })
    @SysLog("查询用户")
    public R info(@LoginUser SysUser user){
        List<String> resources = resourceService.findVisibleResource(user.getId());
        UserLoginDTO uldto = new UserLoginDTO();
        uldto.setId(user.getId());
        uldto.setAccount(user.getAccount());
        uldto.setName(user.getName());
        uldto.setOrgId(user.getOrgId());
        uldto.setStationId(user.getStationId());
        uldto.setMobile(user.getMobile());
        uldto.setPhoto(user.getPhoto());
        uldto.setWorkDescribe(user.getWorkDescribe());
        uldto.setResources(resources);
        return R.ok().data(uldto);
    }

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping("/save")
    @SysLog("新增用户")
    public R save(@LoginUser SysUser user, @Validated UserSaveDTO data){
        userService.saveUser(user,data);
        return R.ok();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PutMapping("/update")
    @SysLog("修改用户")
    public R update(@LoginUser SysUser user,@Validated UserUpdateDTO data){
        userService.updateUser(user,data);
        return R.ok();
    }

    @ApiOperation(value = "修改头像", notes = "修改头像")
    @PutMapping("/avatar")
    @SysLog("修改头像")
    public R avatar(@RequestBody @Validated UserUpdateAvatarDTO data) {
        User user = userService.getById(data.getId());
        user.setAvatar(data.getAvatar());
        userService.updateById(user);
        return R.ok();
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PutMapping("/password")
    @SysLog("修改密码")
    public R updatePassword(@RequestBody UserUpdatePasswordDTO data) {
        userService.updatePassword(data);
        return R.ok();
    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @GetMapping("/reset")
    @SysLog("重置密码")
    public R resetTx(@RequestParam("ids[]") List<Long> ids) {
        userService.reset(ids);
        return R.ok();
    }

    @ApiOperation(value = "删除用户", notes = "根据id物理删除用户")
    @DeleteMapping("/delete")
    @SysLog("删除用户")
    public R delete(@RequestParam("ids[]") List<Long> ids) {
        userService.remove(ids);
        return R.ok();
    }
    /**
     * 查询可用用户
     * @return
     */
    @ApiOperation(value = "查询可用用户", notes = "查询可用用户")
    @GetMapping(value = "/able")
    public R findUserAble(){
        QueryWrapper param = new QueryWrapper();
        param.eq("status",0);
        List<User> list = userService.list();
        return R.ok().data("data",list);
    }
    /**
     * 查询角色的已关联用户
     * @param roleId  角色id
     * @param keyword 账号account或名称name
     */
    @ApiOperation(value = "查询角色的已关联用户", notes = "查询角色的已关联用户")
    @GetMapping(value = "/role")
    public R findUserByRoleId(Long roleId, String keyword) {
        List<User> list = userService.findUserByRoleId(roleId, keyword);
        List<Long> idList = list.stream().mapToLong(User::getId).boxed().collect(Collectors.toList());
        UserRoleDTO userRoleDTO = UserRoleDTO.builder().idList(idList).userList(list).build();
        return R.ok().data("data",userRoleDTO);
    }

    /**
     * feign远程调用 根据账号获取用户
     * @param username
     * @return
     */
    @RequestMapping(value="/selectByAccount", method= RequestMethod.GET)
    R selectByAccount(@RequestParam String username){
        User user = userService.selectByAccount(username);
        return R.ok().data(user);
    }
}
